//
//  ViewController.m
//  三级菜单
//
//  Created by My Mac on 2017/5/10.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "ViewController.h"
#import "Bean.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong) NSMutableArray<Bean *> *marr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self getJsonData];
    
}

-(void) getJsonData
{
    //拿到数据文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"json.json" ofType:nil];
    //将数据转为NSData类型
    NSData *jsonData = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
    //将NSData转为字典
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",dict);
    _marr = [NSMutableArray arrayWithArray:[self addDataToMarr:dict]];
    NSLog(@"_marr:%@",_marr);
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
}

-(NSMutableArray<Bean *> *)addDataToMarr:(NSDictionary *)dict
{
    Bean *bean1;
    Bean *bean2;
    Bean *bean3;
    
    NSMutableArray<Bean *> *marr1 = [NSMutableArray new];
    NSMutableArray<Bean *> *marr2;
    NSMutableArray<Bean *> *marr3;
    
    NSArray *arr1 = dict[@"data"][@"itemtypelist"];
    NSLog(@"-------> arr1:%@",arr1);
    //第一层
    if (arr1 != nil) {
        for (NSDictionary *temp in arr1) {
            bean1 = [Bean new];
            marr2 = [NSMutableArray new];
            marr3 = [NSMutableArray new];
            
            NSArray *arr2 = temp[@"itemtypelist"];
            NSLog(@"-------> arr2:%@",arr2);
            bean1.itemId = temp[@"itemtypeId"];
            bean1.itemName = temp[@"itemtypename"];
            bean1.isOpen = true;
            
            //第二层
            if (arr2 != nil) {
                for (NSDictionary *temp in arr2) {
                    bean2 = [Bean new];
                    
                    NSArray *arr3 = temp[@"itemtypelist"];
                    NSLog(@"-------> arr3:%@",arr3);
                    bean2.itemId = temp[@"itemtypeId"];
                    bean2.itemName = temp[@"itemtypename"];
                    bean2.isOpen = true;
                    bean1.num += 1;
                    
                    //第三层
                    if (arr3 != nil) {
                        for (NSDictionary *temp in arr3) {
                            bean3 = [Bean new];
                            bean3.itemId = temp[@"itemtypeId"];
                            bean3.itemName = temp[@"itemtypename"];
                            bean3.isOpen = true;
                            [marr3 addObject:bean3];
                            bean2.num += 1;
                        }
                        bean2.beanArr = [NSMutableArray arrayWithArray:marr3];
                    }
                    [marr2 addObject:bean2];
                }
                bean1.beanArr = [NSMutableArray arrayWithArray:marr2];
            }
            [marr1 addObject:bean1];
            NSLog(@"marr1:----> %@",marr1);
        }
    }
    return marr1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _marr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCell = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell];
    }
    cell.textLabel.text = _marr[indexPath.row].itemName;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_marr[indexPath.row] isOpen]) {
        [self addItem:indexPath.row];
    } else {
        [self removeItem:_marr[indexPath.row] itemIndex:indexPath.row];
    }
    [_tableView reloadData];
}

-(void)addItem:(NSInteger)i
{
    Bean *bean= _marr[i];
    int a=1;
    if(bean.beanArr.count!=0){
        for (Bean *bean1 in bean.beanArr) {
            [_marr insertObject:bean1 atIndex:i+a];
            NSLog(@"%@",_marr[a].itemName);
            a++;
        }
        bean.isOpen=false;
    }
    bean.isOpen=false;
}

-(void)removeItem:(Bean *)bean itemIndex:(NSInteger)i
{
    if (bean.beanArr.count != 0) {
        for (Bean *bean1 in bean.beanArr) {
            if (!bean.isOpen && bean1.isOpen) {
                [_marr removeObjectAtIndex:i+1];
            } else if (!bean.isOpen && !bean1.isOpen) {
                [_marr removeObjectAtIndex:i+1];
                [self removeItem:bean1 itemIndex:i];
                bean1.isOpen = true;
            }
        }
        bean.isOpen = true;
    }
}

@end
